package mars;

import com.asprise.ocr.Ocr;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;
import sun.net.www.http.HttpClient;

import javax.print.attribute.HashAttributeSet;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 金投会
 * 集账号配置/自动识别验证码
 * 一键登录
 * 一键自动投资
 * 投资策略配置
 *
 */
public class App  extends Base
{
    private static final String baseURL = "https://www.jintouhui.com/";
    //private HttpClient client = new HttpClient();




    private String retrieveCodes(){
        String s = capture();
        while(s.length()==4){
            s = capture();
        }
        return s;
    }

    public void login() {

        String suffix = "user/loginActive.html";
        String url = baseURL + suffix;

        String verifyCode = capture();

        HashMap hashMap = new HashMap();
        hashMap.put("username","taoran");
        hashMap.put("password","zoujing123");
        hashMap.put("verifyCode",verifyCode);

        try {
            CloseableHttpResponse response = HC.post(url,hashMap);
            String result = EntityUtils.toString(response.getEntity());
            ObjectMapper objectMapper = new ObjectMapper();
            Map results = objectMapper.readValue(result,Map.class);
            if("1:验证码错误！".equals(results.get("msg"))){
                System.out.println("验证码错误");
                //login();
            }else{
                System.out.println("登录成功");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取并解析验证码
     * @return
     */
    private String capture(){
        String suffix = "randCode/getVerifyCode.html";
        String url = baseURL + suffix;
        try {
            CloseableHttpResponse response = HC.get(url);
            InputStream inputStream =response.getEntity().getContent();
            File file = new File("d:/a.png");
            OutputStream outputStream = FileUtils.openOutputStream(file);
            IOUtils.copy(inputStream,outputStream);
            Ocr.setUp();
            Ocr ocr = new Ocr();
            ocr.startEngine(Ocr.LANGUAGE_ENG,Ocr.SPEED_SLOW);
            String s = ocr.recognize(new File[]{file},Ocr.RECOGNIZE_TYPE_ALL,Ocr.OUTPUT_FORMAT_PLAINTEXT);
            System.out.println(s);
            ocr.stopEngine();
            return s;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

//    public void myAccount(){
//        String suffix = "accountshow/myaccount.html";
//        String url = baseURL + suffix;
//
//        Request request = new Request.Builder()
//                .url(url)
//                .addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
//                .addHeader("User-Agent","Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1")
//                .addHeader("Connection","keep-alive")
//                .build();
//
//        try {
//            Response response = client.newCall(request).execute();
//            if(response.code()!=200){
//                logger.info("sucker!login failed");
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    /**
     * 访问登录界面，获取相应cookie
     */
    public void iLogin(){
        String url = baseURL+"static/js/utils/track.js";
        HC.get(url);
    }

    public static void main( String[] args ) throws IOException {
        App app  = new App();
        CloseableHttpResponse res = HC.post("https://www.jintouhui.com/user/ilogin.html",null);
        res.close();
        app.iLogin();
        //HC.get("https://www.jintouhui.com/indexsetapk.html");
        app.login();
        //System.out.println(s);
    }
}
